﻿using Microsoft.Xaml.Behaviors;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using VideoTools.Tasks;

namespace VideoTools.Behaviors
{
    class ScaleVideoMultiSelectionBehavior : Behavior<System.Windows.Controls.ListBox>
    {
        // Using a DependencyProperty as the backing store for SelectedItems.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedItemsProperty =
            DependencyProperty.Register("ScaleVideoSelectedItems", typeof(IList), typeof(ScaleVideoMultiSelectionBehavior), new FrameworkPropertyMetadata(null));

        public IList ScaleVideoSelectedItems
        {
            get { return (IList)GetValue(SelectedItemsProperty); }
            set { SetValue(SelectedItemsProperty, value); }
        }

        private void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (AssociatedObject.Items.SourceCollection is null) return;

            ScaleVideoSelectedItems = AssociatedObject.SelectedItems.Cast<ScaleVideoTaskItem>().ToArray();
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;
        }

    }
}
